﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FormCreatPipeInsulation : System.Windows.Forms.Form
	{
		public FormCreatPipeInsulation()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{0AA5EA5E-513E-4F0B-9B19-3069D59C5750}", null);
			base.Icon = Resource.YArchitechIcon;
			this.userSetting = new HVACUserSetting();
			this.uiInfo = this.userSetting.GetPipeInsulationThickness();
			this.InitialUIStatue(this.uiInfo);
		}

		private void FormCreatPipeInsulation_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			this.SetControlsVisible();
			this.beLoad = false;
			this.SetCalcType();
			base.KeyPreview = true;
			this.hyTxtCalcDepth.Text = "0";
		}

		public double GetMediumTemp()
		{
			if (this.rdoKeepWarm.Checked)
			{
				return this.GetWarmMeduimTemp();
			}
			return this.GetDewMeduimTemp();
		}

		public double GetDesignTemp()
		{
			if (this.rdoNoHeating.Checked)
			{
				return this.GetAverageTemp();
			}
			if (this.rdoHeating.Checked)
			{
				return this.GetWinterDesignTemp();
			}
			if (this.rdoWaste.Checked)
			{
				return this.GetEnvironmentTemp();
			}
			if (this.rdoAirCondition.Checked)
			{
				return this.GetConditionDesignTemp();
			}
			return this.GetSummerConditionTemp();
		}

		public double GetDewingTemp()
		{
			if (this.rdoAirCondition.Checked)
			{
				return this.GetSummerConditionDewTemp();
			}
			return this.GetConditionDesignDewTemp();
		}

		private double GetWarmMeduimTemp()
		{
			return this.GetSpecTemp(this.txtWarmMediumTemp);
		}

		private double GetDewMeduimTemp()
		{
			return this.GetSpecTemp(this.txtPreventMediumTemp);
		}

		private double GetAverageTemp()
		{
			return this.GetSpecTemp(this.txtAverageTemp);
		}

		private double GetWinterDesignTemp()
		{
			return this.GetSpecTemp(this.txtWinterDesignTemp);
		}

		private double GetEnvironmentTemp()
		{
			return this.GetSpecTemp(this.txtEnvironmentTemp);
		}

		private double GetSummerConditionTemp()
		{
			return this.GetSpecTemp(this.txtSummerKTTemp);
		}

		private double GetSummerConditionDewTemp()
		{
			return this.GetSpecTemp(this.txtRoomDewTemp);
		}

		private double GetConditionDesignTemp()
		{
			return this.GetSpecTemp(this.txtSummerDesignTemp);
		}

		private double GetConditionDesignDewTemp()
		{
			return this.GetSpecTemp(this.txtHottestDewTemp);
		}

		public InsulationLayerParam GetSettingMaterial()
		{
			InsulationLayerParam result = null;
			string text = this.cmbInsulationMaterial.Text;
			List<InsulationLayerParam> list = this.cmbInsulationMaterial.Tag as List<InsulationLayerParam>;
			if (this.tabControl1.SelectedIndex == 1)
			{
				text = this.cmbDefineInsulationMaterial.Text;
				list = (this.cmbDefineInsulationMaterial.Tag as List<InsulationLayerParam>);
			}
			foreach (InsulationLayerParam insulationLayerParam in list)
			{
				result = insulationLayerParam;
				if (insulationLayerParam.Name.Equals(text))
				{
					result = insulationLayerParam;
					break;
				}
			}
			return result;
		}

		public double GetDefineThickness()
		{
			double result;
			try
			{
				result = Convert.ToDouble(this.hyTxtDefineDepth.Text);
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = 0.0;
			}
			return result;
		}

		public int GetCalcTypeNum()
		{
			int result = 1;
			if (this.tabControl1.SelectedIndex == 1)
			{
				result = 3;
			}
			else if (this.rdoPreventDewing.Checked)
			{
				result = 2;
			}
			return result;
		}

		private double GetSpecTemp(System.Windows.Forms.TextBox txtBox)
		{
			double result = 0.0;
			try
			{
				result = Convert.ToDouble(txtBox.Text);
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = 0.0;
			}
			return result;
		}

		private void InitialUIStatue(DuctInsulationParam uiInfo)
		{
			this.tabControl1.SelectedIndex = uiInfo.TabIndex;
			if (this.rdoPreventDewing.Text == uiInfo.CalcType)
			{
				this.rdoPreventDewing.Checked = true;
				this.rdoKeepWarm.Checked = false;
			}
			else
			{
				this.rdoPreventDewing.Checked = false;
				this.rdoKeepWarm.Checked = true;
			}
			if (this.rdoHeating.Text == uiInfo.Heating)
			{
				this.rdoHeating.Checked = true;
				this.rdoNoHeating.Checked = false;
				this.rdoWaste.Checked = false;
			}
			else if (this.rdoWaste.Text == uiInfo.Heating)
			{
				this.rdoHeating.Checked = false;
				this.rdoNoHeating.Checked = false;
				this.rdoWaste.Checked = true;
			}
			else
			{
				this.rdoHeating.Checked = false;
				this.rdoNoHeating.Checked = true;
				this.rdoWaste.Checked = false;
			}
			if (this.rdoAirCondition.Text == uiInfo.Dewing)
			{
				this.rdoAirCondition.Checked = true;
				this.rdoNoAirCondition.Checked = false;
			}
			else
			{
				this.rdoAirCondition.Checked = false;
				this.rdoNoAirCondition.Checked = true;
			}
			this.BindingCombox(uiInfo.InsulationMaterial);
			this.btnLocation.Text = uiInfo.LocationName;
			this.hyTxtDefineDepth.Text = uiInfo.Thickness.ToString();
			this.txtWinterDesignTemp.Text = uiInfo.WinterDesignTemp.ToString();
			this.txtWarmMediumTemp.Text = uiInfo.WarmMediumTemp.ToString();
			this.txtPreventMediumTemp.Text = uiInfo.PreventMediumTemp.ToString();
			this.txtRoomDewTemp.Text = uiInfo.RoomDewTemp.ToString();
			this.txtSummerDesignTemp.Text = uiInfo.SummerDesignTemp.ToString();
			this.SetBaseRuleValue();
			this.SetWasteTemp();
		}

		private void BindingCombox(string selectMaterial)
		{
			this.operationLocation = new OperationLocation("");
			List<InsulationLayerParam> allInsulationMaterials = this.operationLocation.GetAllInsulationMaterials();
			this.BindingCombox(allInsulationMaterials, this.cmbDefineInsulationMaterial, selectMaterial);
			this.BindingCombox(allInsulationMaterials, this.cmbInsulationMaterial, selectMaterial);
		}

		private void BindingCombox(List<InsulationLayerParam> listMaterial, System.Windows.Forms.ComboBox combox, string selectedMaterial)
		{
			for (int i = 0; i < listMaterial.Count; i++)
			{
				combox.Items.Insert(i, listMaterial[i].Name);
			}
			combox.Tag = listMaterial;
			combox.SelectedItem = selectedMaterial;
		}

		private void SetControlsVisible()
		{
			if (this.rdoKeepWarm.Checked)
			{
				this.SetRadiosVisible(true);
				if (this.rdoHeating.Checked)
				{
					this.SetHeatVisible();
					return;
				}
				if (this.rdoWaste.Checked)
				{
					this.SetWasteVisible();
					return;
				}
				this.SetNoHeatVisible();
				return;
			}
			else
			{
				this.SetRadiosVisible(false);
				if (this.rdoAirCondition.Checked)
				{
					this.SetConditionVisible();
					return;
				}
				this.SetNoConditionVisible();
				return;
			}
		}

		private void SetRadiosVisible(bool beKeepWarm)
		{
			this.rdoNoHeating.Visible = beKeepWarm;
			this.rdoHeating.Visible = beKeepWarm;
			this.rdoWaste.Visible = beKeepWarm;
			this.rdoAirCondition.Visible = !beKeepWarm;
			this.rdoNoAirCondition.Visible = !beKeepWarm;
		}

		private void SetNoHeatVisible()
		{
			this.MediumTempVisible(true);
			this.AverageTempVisible(true);
			this.EnvironmentTempVisible(false);
			this.WinterTempVisible(false);
			this.RoomDewTempVisible(false);
			this.SummerAirControlTempVisible(false);
			this.SummerAirControlDesignTempVisible(false);
			this.HosttestDewTempVisible(false);
		}

		private void SetHeatVisible()
		{
			this.MediumTempVisible(true);
			this.AverageTempVisible(false);
			this.EnvironmentTempVisible(false);
			this.WinterTempVisible(true);
			this.RoomDewTempVisible(false);
			this.SummerAirControlTempVisible(false);
			this.SummerAirControlDesignTempVisible(false);
			this.HosttestDewTempVisible(false);
		}

		private void SetWasteVisible()
		{
			this.MediumTempVisible(true);
			this.AverageTempVisible(false);
			this.EnvironmentTempVisible(true);
			this.WinterTempVisible(false);
			this.RoomDewTempVisible(false);
			this.SummerAirControlTempVisible(false);
			this.SummerAirControlDesignTempVisible(false);
			this.HosttestDewTempVisible(false);
		}

		private void SetConditionVisible()
		{
			this.MediumTempVisible(false);
			this.AverageTempVisible(false);
			this.EnvironmentTempVisible(false);
			this.WinterTempVisible(false);
			this.RoomDewTempVisible(false);
			this.SummerAirControlTempVisible(false);
			this.SummerAirControlDesignTempVisible(true);
			this.HosttestDewTempVisible(true);
		}

		private void SetNoConditionVisible()
		{
			this.MediumTempVisible(false);
			this.AverageTempVisible(false);
			this.EnvironmentTempVisible(false);
			this.WinterTempVisible(false);
			this.RoomDewTempVisible(true);
			this.SummerAirControlTempVisible(true);
			this.SummerAirControlDesignTempVisible(false);
			this.HosttestDewTempVisible(false);
		}

		private void MediumTempVisible(bool beKeepWarm)
		{
			this.txtPreventMediumTemp.Visible = !beKeepWarm;
			this.txtWarmMediumTemp.Visible = beKeepWarm;
		}

		private void AverageTempVisible(bool beShow)
		{
			this.txtAverageTemp.Visible = beShow;
			this.lblAverageTemp.Visible = beShow;
			this.txtAverageTemp.ReadOnly = beShow;
		}

		private void EnvironmentTempVisible(bool beShow)
		{
			this.lblEnvironment.Visible = beShow;
			this.txtEnvironmentTemp.Visible = beShow;
			this.txtEnvironmentTemp.ReadOnly = beShow;
		}

		private void WinterTempVisible(bool beShow)
		{
			this.lblWinterDesignTemp.Visible = beShow;
			this.txtWinterDesignTemp.Visible = beShow;
		}

		private void RoomDewTempVisible(bool beShow)
		{
			this.lblDesignDewingTemp.Visible = beShow;
			this.txtHottestDewTemp.Visible = beShow;
			this.txtHottestDewTemp.ReadOnly = beShow;
		}

		private void SummerAirControlTempVisible(bool beShow)
		{
			this.lblACDesignTemp.Visible = beShow;
			this.txtSummerKTTemp.Visible = beShow;
			this.txtSummerKTTemp.ReadOnly = beShow;
		}

		private void SummerAirControlDesignTempVisible(bool beShow)
		{
			this.lblAirConditionTemp.Visible = beShow;
			this.txtSummerDesignTemp.Visible = beShow;
		}

		private void HosttestDewTempVisible(bool beShow)
		{
			this.lblHottestDewTemp.Visible = beShow;
			this.txtRoomDewTemp.Visible = beShow;
		}

		private void SetCalcType()
		{
			if (this.rdoPreventDewing.Checked)
			{
				if (this.rdoAirCondition.Text == this.uiInfo.Dewing)
				{
					this.rdoAirCondition.Checked = true;
					return;
				}
				this.rdoNoAirCondition.Checked = true;
				return;
			}
			else
			{
				if (this.rdoHeating.Text == this.uiInfo.Heating)
				{
					this.rdoHeating.Checked = true;
					return;
				}
				if (this.rdoWaste.Text == this.uiInfo.Heating)
				{
					this.rdoWaste.Checked = true;
					return;
				}
				this.rdoNoHeating.Checked = true;
				return;
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			FormCreatPipeInsulation.SendMessage(CreatPipeInsulation.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormCreatPipeInsulation.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormCreatPipeInsulation.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private double GetSpecialTemp(double defaultTemp, string txtValue)
		{
			double result;
			try
			{
				result = Convert.ToDouble(txtValue);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = defaultTemp;
			}
			return result;
		}

		private void SetWasteTemp()
		{
			string text = this.txtWarmMediumTemp.Text;
			double doubleByString = this.GetDoubleByString(text);
			if (doubleByString < 80.0)
			{
				this.txtEnvironmentTemp.Text = "20";
				return;
			}
			if (doubleByString >= 80.0 && doubleByString <= 110.0)
			{
				this.txtEnvironmentTemp.Text = "30";
				return;
			}
			this.txtEnvironmentTemp.Text = "40";
		}

		private double GetDoubleByString(string txtValue)
		{
			double result = 0.0;
			if (string.IsNullOrEmpty(txtValue))
			{
				return result;
			}
			try
			{
				result = Convert.ToDouble(txtValue);
			}
			catch (Exception)
			{
				result = 0.0;
			}
			return result;
		}

		private void SetBaseRuleValue()
		{
			LocationWeather weatherByLocationName = this.operationLocation.GetWeatherByLocationName(this.btnLocation.Text);
			this.txtAverageTemp.Text = weatherByLocationName.AverageTemp.ToString();
			this.txtSummerKTTemp.Text = weatherByLocationName.ConditionTemp.ToString();
			this.txtHottestDewTemp.Text = weatherByLocationName.DewPointTemp.ToString();
		}

		private void cmbInsulationMaterial_SelectedIndexChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.ComboBox comboBox = sender as System.Windows.Forms.ComboBox;
			if (comboBox == null)
			{
				return;
			}
			this.uiInfo.InsulationMaterial = comboBox.Text;
			this.cmbDefineInsulationMaterial.Text = comboBox.Text;
			this.cmbInsulationMaterial.Text = comboBox.Text;
		}

		private void hyTxtDefineDepth_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox == null || !textBox.Visible)
			{
				return;
			}
			if (textBox.Name == "txtWarmMediumTemp" || "txtWinterDesignTemp" == textBox.Name || "txtPreventMediumTemp" == textBox.Name || textBox.Name == "txtRoomDewTemp" || textBox.Name == "txtSummerDesignTemp" || textBox.Name == "hyTxtDefineDepth")
			{
				string text = this.CheckTextValue(textBox);
				if (!string.IsNullOrEmpty(text))
				{
					YJKMessageBox.Warning(text);
					textBox.SelectAll();
					textBox.Focus();
					return;
				}
			}
			if (textBox.Name == "txtWarmMediumTemp" && this.rdoWaste.Checked)
			{
				this.SetWasteTemp();
			}
			try
			{
				this.uiInfo.Thickness = Convert.ToDouble(this.hyTxtDefineDepth.Text);
				if (textBox.Name == "hyTxtDefineDepth" && this.uiInfo.Thickness < 0.0)
				{
					YJKMessageBox.Warning("保温层不能为负值！");
				}
			}
			catch (Exception ex)
			{
				this.uiInfo.Thickness = 50.0;
				string message = ex.Message;
			}
		}

		private void btnLocation_Click(object sender, EventArgs e)
		{
			FormObtainLocation formObtainLocation = new FormObtainLocation(this.btnLocation.Text);
			if (formObtainLocation.ShowDialog() == DialogResult.OK)
			{
				this.btnLocation.Text = formObtainLocation.GetSelectedLocation();
			}
		}

		private void btnLocation_TextChanged(object sender, EventArgs e)
		{
			this.uiInfo.LocationName = this.btnLocation.Text;
			this.SetBaseRuleValue();
		}

		private void rdoKeepWarm_CheckedChanged(object sender, EventArgs e)
		{
			if (this.beLoad)
			{
				return;
			}
			RadioButton radioButton = sender as RadioButton;
			if (radioButton == null || !radioButton.Checked)
			{
				return;
			}
			this.SetControlsVisible();
			if (radioButton.Name == "rdoKeepWarm" || radioButton.Name == "rdoPreventDewing")
			{
				this.uiInfo.CalcType = radioButton.Text;
			}
			if (radioButton.Name == "rdoNoHeating" || radioButton.Name == "rdoHeating" || radioButton.Name == "rdoWaste")
			{
				this.uiInfo.Heating = radioButton.Text;
			}
			if (radioButton.Name == "rdoNoAirCondition" || radioButton.Name == "rdoAirCondition")
			{
				this.uiInfo.Dewing = radioButton.Text;
			}
			this.SetCalcType();
			this.SetWasteTemp();
		}

		private void txtPreventMediumTemp_TextChanged(object sender, EventArgs e)
		{
			if (this.beLoad)
			{
				return;
			}
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox == null || !textBox.Visible)
			{
				return;
			}
			if (textBox.Name == "txtWarmMediumTemp")
			{
				this.uiInfo.WarmMediumTemp = this.GetSpecialTemp(30.0, textBox.Text);
			}
			if (textBox.Name == "txtPreventMediumTemp")
			{
				this.uiInfo.PreventMediumTemp = this.GetSpecialTemp(15.0, textBox.Text);
			}
			if (textBox.Name == "txtWinterDesignTemp")
			{
				this.uiInfo.WinterDesignTemp = this.GetSpecialTemp(20.0, textBox.Text);
			}
			if (textBox.Name == "txtRoomDewTemp")
			{
				this.uiInfo.RoomDewTemp = this.GetSpecialTemp(20.0, textBox.Text);
			}
			if (textBox.Name == "txtSummerDesignTemp")
			{
				this.uiInfo.SummerDesignTemp = this.GetSpecialTemp(26.0, textBox.Text);
			}
			this.SetWasteTemp();
		}

		private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.uiInfo.TabIndex = this.tabControl1.SelectedIndex;
		}

		private void FormCreatPipeInsulation_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetfocusMessage();
				return;
			}
			base.Focus();
		}

		private void FormCreatPipeInsulation_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.userSetting.WriteCalcPipeInsulationThickness(this.uiInfo);
			this.ChangeOperateState();
			this.ChangeOperateState();
		}

		private string CheckTextValue(System.Windows.Forms.TextBox txtBox)
		{
			string result = "";
			string text = txtBox.Text;
			double num = 0.0;
			if (!this.BeNumber(text, ref num))
			{
				result = "文本框值必须为数字！";
			}
			if (num <= 0.0)
			{
				result = "输入数字必须为大于零的正数！";
			}
			string text2 = StringTools.CutOffEfficacious(num.ToString(), 1);
			if (!num.ToString().Equals(text2))
			{
				txtBox.Text = text2;
				result = "输入数字仅允许小数点后一位！";
			}
			return result;
		}

		private bool BeNumber(string txtValue, ref double txtNumber)
		{
			bool result = true;
			try
			{
				txtNumber = Convert.ToDouble(txtValue);
				result = true;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = false;
			}
			return result;
		}

		private void FormCreatPipeInsulation_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				base.Close();
			}
		}

		private void txtPreventMediumTemp_Leave(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox == null || !textBox.Visible)
			{
				return;
			}
			if (textBox.Name == "txtWarmMediumTemp" || "txtWinterDesignTemp" == textBox.Name || "txtPreventMediumTemp" == textBox.Name || textBox.Name == "txtRoomDewTemp" || textBox.Name == "txtSummerDesignTemp" || textBox.Name == "hyTxtDefineDepth")
			{
				string text = this.CheckTextValue(textBox);
				if (!string.IsNullOrEmpty(text))
				{
					YJKMessageBox.Warning(text);
					textBox.SelectAll();
					textBox.Focus();
					return;
				}
			}
		}

		private void txtPreventMediumTemp_Validating(object sender, CancelEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox == null || !textBox.Visible)
			{
				return;
			}
			if (textBox.Name == "txtWarmMediumTemp" || "txtWinterDesignTemp" == textBox.Name || "txtPreventMediumTemp" == textBox.Name || textBox.Name == "txtRoomDewTemp" || textBox.Name == "txtSummerDesignTemp" || textBox.Name == "hyTxtDefineDepth")
			{
				string text = this.CheckTextValue(textBox);
				if (!string.IsNullOrEmpty(text))
				{
					YJKMessageBox.Warning(text);
					textBox.SelectAll();
					textBox.Focus();
					return;
				}
			}
		}

		private void FormCreatPipeInsulation_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private OperationLocation operationLocation;

		private HVACUserSetting userSetting;

		private bool beLoad = true;

		private DuctInsulationParam uiInfo;

		private ControlInfoManager m_thisManger;
	}
}
